from main_yolov3 import *
import requests
import time
import threading

# url = "http://192.168.0.100:8081"
# 打开网络摄像头
# cap = cv2.VideoCapture(url)


# cap = cv.VideoCapture("http://127.0.0.1:8080/?action=stream")
# cap = cv.VideoCapture("http://127.0.0.1:8080/?action=stream")

cap = cv.VideoCapture('xbd.mp4')

# 创建可以调节大小的窗口
# cv.namedWindow("video",0)

# cap = cv.VideoCapture(0)

# cap.set(cv.CAP_PROP_BUFFERSIZE, 3)
cap.set(cv.CAP_PROP_FPS, 60)
# cap.set(3, 320)
# cap.set(4, 320)


def get(url):
    header = {'Content-Type':'application/json'}
    response = requests.get(url,header)
    return response.json()

RTip = True
def read_frame():
    global frame, RTip, cap
    while RTip:
        _, frame = cap.read()
 
th1 = threading.Thread(target=read_frame, name='Thread_1')
th1.start()
time.sleep(3)

while(1):
    global frame
    net = cv.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
    net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)
    net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)

    blob = cv.dnn.blobFromImage(frame, 1/255.0, (inpWidth, inpHeight), [0, 0, 0], swapRB=False, crop=False)

    net.setInput(blob)
    outs = net.forward(getOutputsNames(net))

    postprocess(frame, outs)
    t, _ = net.getPerfProfile()
    label = 'Inference time: %.2f ms' % (t * 1000.0 / cv.getTickFrequency())
    cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))

    cv.imshow("video",frame) 

    if (cv.waitKey(1) & 0xFF == ord('q') ):
        break
cap.release()
cv.destroyAllWindows()

